package com.wanchuang.hepos.ui.page.home;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.protobuf.InvalidProtocolBufferException;
import com.wanchuang.hepos.R;
import com.wanchuang.hepos.bridge.data.repository.Apis;
import com.wanchuang.hepos.bridge.request.HomeRequestViewModel;
import com.wanchuang.hepos.bridge.state.home.TiXianClickViewModel;
import com.wanchuang.hepos.bridge.state.mine.MessageViewModel;
import com.wanchuang.hepos.databinding.FragmentMessageBinding;
import com.wanchuang.hepos.databinding.FragmentTiXianClickBinding;
import com.wanchuang.hepos.net.NetworkManager;
import com.wanchuang.hepos.net.interfaces.OnRequestCallBack;
import com.wanchuang.hepos.proto.MemberBank;
import com.wanchuang.hepos.proto.MemberOuterClass;
import com.wanchuang.hepos.proto.Settle;
import com.wanchuang.hepos.ui.base.BaseFragment;
import com.wanchuang.hepos.ui.utils.StringReplaceUtil;

/**
 * 文件名 : HomeRootFragment
 * 创建者 : MangoWang
 * 创建日期 : 2019/12/24 16:20
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */
public class TiXianClickFragment extends BaseFragment {


    private FragmentTiXianClickBinding mBinding;
    private TiXianClickViewModel mViewModel;
    private HomeRequestViewModel mRequestViewModel;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mViewModel = ViewModelProviders.of(this).get(TiXianClickViewModel.class);
        mRequestViewModel = ViewModelProviders.of(this).get(HomeRequestViewModel.class);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_ti_xian_click, container, false);
        mBinding = FragmentTiXianClickBinding.bind(view);
        mBinding.setClick(new ClickProxy());
        mBinding.setVm(mViewModel);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mRequestViewModel.getErrorString().observe(this, s -> {
            hideProgress();
            showShortToast(s);
        });
        mRequestViewModel.getResultString().observe(this, s -> {
            hideProgress();
            showShortToast(s);
        });


        NetworkManager.INSTANCE.post(Apis.get_bank, new OnRequestCallBack() {
            @Override
            public void onOk(byte[] bytes) throws InvalidProtocolBufferException {
                MemberBank.get_bank get_bank = MemberBank.get_bank.parseFrom(bytes);
                mViewModel.bankName.set(get_bank.getArrayList().get(0).getBankName());
                mViewModel.bankNumber.set(StringReplaceUtil.bankCardReplaceWithStar(get_bank.getArrayList().get(0).getBankCard()));
            }

            @Override
            public void onError(int errorCode, String errorMessage) {

            }
        });


    }

    @Override
    public void loadInitData() {
        super.loadInitData();
    }

    public class ClickProxy {
        public void back() {
            nav().navigateUp();
        }
        public void tixian() {


            if (TextUtils.isEmpty(mViewModel.number.get()))
            {
                showShortToast("请输入提现金额");
                return;
            }


            Settle.trade.Builder builder = Settle.trade.newBuilder();
            builder.setAmount(mViewModel.number.get());
            NetworkManager.INSTANCE.post(Apis.cash_add, builder.build().toByteArray(),new OnRequestCallBack() {
                @Override
                public void onOk(byte[] bytes) throws InvalidProtocolBufferException {


                }

                @Override
                public void onError(int errorCode, String errorMessage) {

                    if (errorCode == 200)
                    {
                        mSharedViewModel.money.setValue(mViewModel.number.get());
                        nav().navigate(R.id.action_tiXianClickFragment_to_tiXianDetailFragment);

                    }else
                    {
                        showShortToast(errorMessage);
                    }

                }
            });

        }


    }

    @Override
    public void onHiddenChanged(boolean hidden) { }
}
